import { requestJSON } from "@/js/url";
import addSlot from "./addSlot";
import addPort from './addPort'
class startDrawCanvas {
  constructor (param) {
    this.param = param
    this.ratio = param.ratio;
    this.get(this.param.node.id)

    this.ctx = param.canvas.getContext('2d');
    console.log("设备ID：", param)
  }
  cabinetInfoUrl (id) {
    return `/equipment_view_info/${id}?towards=0`;
  }
  async get(eq_id){
    let data = (await requestJSON({ url: this.cabinetInfoUrl(eq_id) })).data;
    console.log("设备 ID:", data)

    let ports = data.port
    let slot = data.slot;

    this.setBoardToSlot(data)

    //this.drawPort(ports, 'green')
    new addPort({
      ports:ports,
      color: 'green',
      ratio: this.ratio,
      ctx: this.ctx
    }).draw()
    this.drawSlot(slot, 'red')

    if(this.param.texture) this.param.texture.needsUpdate = true

  }

  setBoardToSlot (data) {
    data.board.map((board)=>{
      try{
        data.slot[board.slot_index]['boardPort'] =  board
      }catch(e){
        console.log(e)
      }
      
    })
  }

  async drawSlot (ports, color) {
    
    for(let i=0; i<ports.length; i++){
      let port = ports[i]
       // 绘制一个 1x1 像素的红点
     // this.ctx.fillRect(20, 20, 20, 10); // 绘制一个 1x1 像素的红点
     console.log("port:", port)
     if(port['boardPort']){
        await new addSlot({
          ctx: this.ctx,
          ratio: this.ratio
        }).draw(port)
     }
      
    }  
    
    if(this.param.texture) this.param.texture.needsUpdate = true
  }

  
}

export default startDrawCanvas